#include "Defines.h"
#include "AI.h"

void AI::populateBoard(char** board)
{
	for(int r = 0; r < HEIGHT; ++r)
	{
		for(int c = 0; c < WIDTH; ++c)
		{
			aiCharBoard[r][c] = board[r][c];
		}
	}
}

void AI::chooseMove(short & x, short & y, char & input)
{
	int max = 0, curr = 0;
	x = y = 0;
	for(int row = 0; row < HEIGHT; ++row)
	{
		for(int col = 0; col < WIDTH; ++col)
		{
			curr = legalMove(row, col);
			if(curr > max)
			{
				max = curr;
				x = row;
				y = col;
			}
		}
	}
	if(max)
		input = '\r';
	else
		input = 'p';
}

int AI::legalMove(int x, int y)
{
	int maxPieces = 0;
	int currPieces = 0;

	if(aiCharBoard[y][x] != 'g')
	{
		return 0;
	}

	bool line = false, oppFound = false;

	//Check to the right
	for(int i = x + 1; i < WIDTH; ++i)
	{
		if(aiCharBoard[y][i] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[y][i] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[y][i] == oppColor && (i + 1) < WIDTH)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check to the left
	for(int i = x - 1; i >= 0; --i)
	{
		if(aiCharBoard[y][i] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[y][i] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[y][i] == oppColor && (i - 1) >= 0)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check above
	for(int i = y - 1; i >= 0; --i)
	{
		if(aiCharBoard[i][x] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[i][x] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[i][x] == oppColor && (i - 1) >= 0)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check below
	for(int i = y + 1; i < HEIGHT; ++i)
	{
		if(aiCharBoard[i][x] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[i][x] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[i][x] == oppColor && (i + 1) < HEIGHT)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check below and to the right
	for(int i = y + 1, j = x + 1; i < HEIGHT && j < WIDTH; ++i, ++j)
	{
		if(aiCharBoard[i][j] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[i][j] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[i][j] == oppColor && (i + 1) < HEIGHT && (j + 1) < WIDTH)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check below and to the left
	for(int i = y + 1, j = x - 1; i < HEIGHT && j > 0; ++i, --j)
	{
		if(aiCharBoard[i][j] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[i][j] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[i][j] == oppColor && (i + 1) < HEIGHT && (j - 1) > 0)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check up and to the right
	for(int i = y - 1, j = x + 1; i > 0 && j < WIDTH; --i, ++j)
	{
		if(aiCharBoard[i][j] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[i][j] == selfColor&& oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[i][j] == oppColor && (i - 1) > 0 && (j + 1) < WIDTH)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;

	//Check up and to the left
	for(int i = y - 1, j = x - 1; i > 0 && j > 0; --i, --j)
	{
		if(aiCharBoard[i][j] == 'g')
		{
			currPieces = 0;
			line = false;
			break;
		}
		else if(aiCharBoard[i][j] == selfColor && oppFound)
		{
			line = true;
			break;
		}
		else if(aiCharBoard[i][j] == oppColor && (i - 1) > 0 && (j - 1) > 0)
		{
			++currPieces;
			oppFound = true;
		}
		else
		{
			currPieces = 0;
			break;
		}
	}

	maxPieces += currPieces;
	currPieces = 0;
	oppFound = false;
	line = false;
	
	return maxPieces;
}

void AI::createArray()
{
	aiCharBoard = new char*[HEIGHT];
	for(int i = 0; i < HEIGHT; ++i)
	{
		aiCharBoard[i] = new char[WIDTH];
	}
}